/** Keriosity, ESIR
 * 	ManuelActivity (VUE): activité permettant de commander notre robot grace au bluetooth ou au wifi
 *	@author: GOUYET Brett
 *	@date: 10/03/2012, deniere modif: 19/05 
 */

package keriosity.activities;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import keriosity.core.IManual;
import keriosity.core.Manual;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
import com.activity.keriosity.R;


public class ManualActivity extends Activity implements IManualObserver{
	
	protected IManual _manual = null; //notre controleur

	private Button _moveMode = null;
	protected int _ctpSend = 0;
	
	private Button _menu = null;
	private Button _auto = null;
	
	private EditText _nameRobot = null; 
	private Button _connexionBluetooth = null;	
	private Button _connexionWifi = null;
	private Button _deconnexion = null;
	private Button _prendrePhoto = null;
		
	private Button _stop = null;	
	private Button _move = null;
	private Button _right = null;
	private Button _back = null;
	private Button _left = null;
	
	private SeekBar _seekBarVitesse = null;
	private int k_vitesse = 1; //vitesse du robot de 1 a 4
				
		
	protected void onCreate(Bundle savedInstanceState) {
			
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manual);
			
		_manual = new Manual(this, true);
		
			/** WIDGETS **/
		_moveMode = (Button) findViewById(R.id.manuel_moveMode);
		
		_menu = (Button) findViewById(R.id.menu);
		_auto = (Button) findViewById(R.id.automatic);
		
		_nameRobot = (EditText)findViewById(R.id.manuel_nameConnexion);
		_connexionBluetooth = (Button) findViewById(R.id.manuel_bluetooth);	
		_connexionWifi = (Button) findViewById(R.id.manuel_wifi);	
		_deconnexion = (Button) findViewById(R.id.manuel_deconnexion);
		_prendrePhoto = (Button) findViewById(R.id.prendrePhoto);
						
		_stop = (Button) findViewById(R.id.manuel_stop);
		_move = (Button) findViewById(R.id.manuel_move);
		_right = (Button) findViewById(R.id.manuel_right);
		_back = (Button) findViewById(R.id.manuel_back);		
		_left = (Button) findViewById(R.id.manuel_left);		
		
		_seekBarVitesse = (SeekBar) findViewById(R.id.vitesse);
		
		
				/** LISTENERS DES BOUTTONS **/
		
			/** Boutton pour activer le mode move **/
		_moveMode.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		
				_manual.activateSensor(!_manual.isSensorActivated());
				
				if(_manual.isSensorActivated())
					Toast.makeText(getApplicationContext(),getString(R.string.gyroscope_activate), Toast.LENGTH_SHORT).show();    
				else
					Toast.makeText(getApplicationContext(),getString(R.string.gyroscope_desactivate), Toast.LENGTH_SHORT).show();  
		}});//_moveMode
		
		
			/** Bouttons de connections **/
					
		_menu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		
				_manual.disconnect(); //on se deconnecte
				
				Intent intent = new Intent(ManualActivity.this, HomeActivity.class); //on change d'activite
				startActivity(intent);
		}});//_menu
		
		_auto.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		
				_manual.disconnect(); //on se deconnecte
				
				Intent intent = new Intent(ManualActivity.this, AutoActivity.class); //on change d'activite
				startActivity(intent);
		}});//_menu

		_connexionBluetooth.setOnClickListener(new OnClickListener() {
	       	
	    	public void onClick(View v) {
	    		_manual.disconnect();   //on se deconnecte  
	    		
	    		//on recupere le nom du robot et on se connecte
	    		if(_manual.connectWithBluetooth(_nameRobot.getText().toString())) //on se connecte         		
	    			Toast.makeText(getApplicationContext(),getString(R.string.connection_bluetooth), Toast.LENGTH_SHORT).show();      
	    		else        			
	    			Toast.makeText(getApplicationContext(),getString(R.string.error_bluetooth), Toast.LENGTH_SHORT).show();         		
	    	}
		}); 
				
		_connexionWifi.setOnClickListener(new OnClickListener() {
           	
        	public void onClick(View v) {
        		_manual.disconnect();   //on se deconnecte  
        		
        		//on recupere le nom du robot et on se connecte
        		if(_manual.connectWithWifi( _nameRobot.getText().toString())) //on se connecte    			
        			Toast.makeText(getApplicationContext(),getString(R.string.connection_wifi), Toast.LENGTH_SHORT).show();         		
        		else        			
        			Toast.makeText(getApplicationContext(),getString(R.string.error_wifi), Toast.LENGTH_SHORT).show();         		
        	}
		});
		
		_deconnexion.setOnClickListener(new OnClickListener() {
           	
        	public void onClick(View v) {        		       		
        		_manual.disconnect();   //on se deconnecte  
        		Toast.makeText(getApplicationContext(),getString(R.string.deconnection), Toast.LENGTH_SHORT).show();        
        	}
		});
		
		_prendrePhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				send("takephoto&address=kevin.texier2@gmail.com");
			}
		});
		
		
			/** Bouttons de directions **/
		
		//on utilise les bouttons si on a desactive le mode moving 
		_stop.setOnClickListener(new OnClickListener() {public void onClick(View v) { if(!_manual.isSensorActivated()) send("e");}}); 
		
		_move.setOnClickListener(new OnClickListener() {public void onClick(View v) { if(!_manual.isSensorActivated()) send("z");}}); 	      
        
		_right.setOnClickListener(new OnClickListener() {public void onClick(View v) { if(!_manual.isSensorActivated()) send("d");}}); 		
                 
		_back.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!_manual.isSensorActivated())
					send("s");

			}
		});	        
        
        _left.setOnClickListener(new OnClickListener() {public void onClick(View v) { if(!_manual.isSensorActivated()) send("q");}}); 
        
             
        	/** SEEKBAR GERANT LA VITESSE **/
        
		_seekBarVitesse.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					
			public void onStopTrackingTouch(SeekBar arg0) {}			
			
			public void onStartTrackingTouch(SeekBar arg0) {}
			
			//on va envoyer la vitesse selon la progression de la seekbar
			public void onProgressChanged(SeekBar seekbar, int progression, boolean arg2) {
							
			
				if(progression <= 2 && k_vitesse != 1){ //premiere vitesse
					k_vitesse = 1;
					send("1");
				}
					
				if(progression > 2 && progression <= 5 && k_vitesse != 2){ //deuxieme vitesse
					k_vitesse = 2;
					send("2");
				}
								
				if(progression > 5 && progression <= 8 && k_vitesse != 3){ //troisieme vitesse
					k_vitesse = 3;
					send("3");
				}
				
				if(progression > 8 && k_vitesse != 4){ //quatrieme vitesse
					k_vitesse = 4;
					send("4");
				}				
			}				
		});//_seekBarVitesse
	}
	
	public void send(String order){
		
		//on envoie l'ordre avec les donnees de missions et on traite les eventuelles erreurs
		switch (_manual.send(order)){
		
			case 0:
				_ctpSend = 0;
		    break;
		    
			case -1:
				_ctpSend = 0;
				Toast.makeText(getApplicationContext(),getString(R.string.error_connexion_1), Toast.LENGTH_SHORT).show();    
		    break;
		    
			case 1:
				if(_ctpSend < 1){
					_ctpSend++;
					Toast.makeText(getApplicationContext(),getString(R.string.error_connexion_2), Toast.LENGTH_SHORT).show();
				}
		    break;
		}
	}
	
	
	
	/** On renvoi le contexte de l'activity **/
	public Context getContext(){
		return getApplicationContext();
	}	
	
	/** On modifie l'opacite des boutons **/
	public void setOpaqueMovingBouttons(int opacite){
		
		//opacite: 0 transparent ->  255 opaque
		_stop.getBackground().setAlpha(opacite); 
		_move.getBackground().setAlpha(opacite); 
		_right.getBackground().setAlpha(opacite); 
		_back.getBackground().setAlpha(opacite); 
		_left.getBackground().setAlpha(opacite); 
	}
	
	public void setOpaqueStop(int opacite){_stop.getBackground().setAlpha(opacite);}
	public void setOpaqueMove(int opacite){_move.getBackground().setAlpha(opacite);}
	public void setOpaqueRight(int opacite){_right.getBackground().setAlpha(opacite);}
	public void setOpaqueBack(int opacite){_back.getBackground().setAlpha(opacite);}
	public void setOpaqueLeft(int opacite){_left.getBackground().setAlpha(opacite);}
	
}
